home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / BarChart / Chart.class (.txt) next >
Encoding:
Java Class File  |  1995-10-12  |  5.3 KB  |  210 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics;
  7.  
  8. public class Chart extends Applet {
  9.    static final int VERTICAL = 0;
  10.    static final int HORIZONTAL = 1;
  11.    static final int SOLID = 0;
  12.    static final int STRIPED = 1;
  13.    int orientation;
  14.    String title;
  15.    Font titleFont;
  16.    FontMetrics titleFontMetrics;
  17.    int titleHeight = 15;
  18.    int columns;
  19.    int[] values;
  20.    Object[] colors;
  21.    Object[] labels;
  22.    int[] styles;
  23.    int scale = 10;
  24.    int maxLabelWidth;
  25.    int barWidth;
  26.    int barSpacing = 10;
  27.    int max;
  28.  
  29.    public synchronized void init() {
  30.       this.titleFont = new Font("Courier", 1, 12);
  31.       this.titleFontMetrics = ((Component)this).getFontMetrics(this.titleFont);
  32.       this.title = ((Applet)this).getParameter("title");
  33.       if (this.title == null) {
  34.          this.title = "Chart";
  35.       }
  36.  
  37.       String rs = ((Applet)this).getParameter("columns");
  38.       if (rs == null) {
  39.          this.columns = 5;
  40.       } else {
  41.          this.columns = Integer.parseInt(rs);
  42.       }
  43.  
  44.       rs = ((Applet)this).getParameter("scale");
  45.       if (rs == null) {
  46.          this.scale = 10;
  47.       } else {
  48.          this.scale = Integer.parseInt(rs);
  49.       }
  50.  
  51.       rs = ((Applet)this).getParameter("orientation");
  52.       if (rs == null) {
  53.          this.orientation = 0;
  54.       } else if (rs.toLowerCase().equals("vertical")) {
  55.          this.orientation = 0;
  56.       } else if (rs.toLowerCase().equals("horizontal")) {
  57.          this.orientation = 1;
  58.       } else {
  59.          this.orientation = 0;
  60.       }
  61.  
  62.       this.values = new int[this.columns];
  63.       this.colors = new Color[this.columns];
  64.       this.labels = new String[this.columns];
  65.       this.styles = new int[this.columns];
  66.  
  67.       for(int i = 0; i < this.columns; ++i) {
  68.          rs = ((Applet)this).getParameter("C" + (i + 1));
  69.          if (rs != null) {
  70.             try {
  71.                this.values[i] = Integer.parseInt(rs);
  72.             } catch (NumberFormatException var3) {
  73.                this.values[i] = 0;
  74.             }
  75.          }
  76.  
  77.          if (this.values[i] > this.max) {
  78.             this.max = this.values[i];
  79.          }
  80.  
  81.          rs = ((Applet)this).getParameter("C" + (i + 1) + "_label");
  82.          this.labels[i] = rs == null ? "" : rs;
  83.          this.maxLabelWidth = Math.max(this.titleFontMetrics.stringWidth((String)this.labels[i]), this.maxLabelWidth);
  84.          rs = ((Applet)this).getParameter("C" + (i + 1) + "_style");
  85.          if (rs != null && !rs.toLowerCase().equals("solid")) {
  86.             if (rs.toLowerCase().equals("striped")) {
  87.                this.styles[i] = 1;
  88.             } else {
  89.                this.styles[i] = 0;
  90.             }
  91.          } else {
  92.             this.styles[i] = 0;
  93.          }
  94.  
  95.          rs = ((Applet)this).getParameter("C" + (i + 1) + "_color");
  96.          if (rs != null) {
  97.             if (rs.equals("red")) {
  98.                this.colors[i] = Color.red;
  99.             } else if (rs.equals("green")) {
  100.                this.colors[i] = Color.green;
  101.             } else if (rs.equals("blue")) {
  102.                this.colors[i] = Color.blue;
  103.             } else if (rs.equals("pink")) {
  104.                this.colors[i] = Color.pink;
  105.             } else if (rs.equals("orange")) {
  106.                this.colors[i] = Color.orange;
  107.             } else if (rs.equals("magenta")) {
  108.                this.colors[i] = Color.magenta;
  109.             } else if (rs.equals("cyan")) {
  110.                this.colors[i] = Color.cyan;
  111.             } else if (rs.equals("white")) {
  112.                this.colors[i] = Color.white;
  113.             } else if (rs.equals("yellow")) {
  114.                this.colors[i] = Color.yellow;
  115.             } else if (rs.equals("gray")) {
  116.                this.colors[i] = Color.gray;
  117.             } else if (rs.equals("darkGray")) {
  118.                this.colors[i] = Color.darkGray;
  119.             } else {
  120.                this.colors[i] = Color.gray;
  121.             }
  122.          } else {
  123.             this.colors[i] = Color.gray;
  124.          }
  125.       }
  126.  
  127.       switch (this.orientation) {
  128.          case 0:
  129.          default:
  130.             this.barWidth = this.maxLabelWidth;
  131.             ((Component)this).resize(Math.max(this.columns * (this.barWidth + this.barSpacing), this.titleFontMetrics.stringWidth(this.title)) + this.titleFont.getSize() + 5, this.max * this.scale + 2 * this.titleFont.getSize() + 5 + this.titleFont.getSize());
  132.             return;
  133.          case 1:
  134.             this.barWidth = this.titleFont.getSize();
  135.             ((Component)this).resize(Math.max(this.max * this.scale + this.titleFontMetrics.stringWidth("" + this.max), this.titleFontMetrics.stringWidth(this.title)) + this.maxLabelWidth + 5, this.columns * (this.barWidth + this.barSpacing) + this.titleFont.getSize() + 10);
  136.       }
  137.    }
  138.  
  139.    public synchronized void paint(Graphics g) {
  140.       g.setColor(Color.black);
  141.       int i = this.titleFontMetrics.stringWidth(this.title);
  142.       g.setFont(this.titleFont);
  143.       g.drawString(this.title, Math.max((((Component)this).size().width - i) / 2, 0), ((Component)this).size().height - this.titleFontMetrics.getDescent());
  144.  
  145.       for(int var8 = 0; var8 < this.columns; ++var8) {
  146.          switch (this.orientation) {
  147.             case 0:
  148.             default:
  149.                int cx = Math.max(this.barWidth + this.barSpacing, this.maxLabelWidth) * var8 + this.barSpacing;
  150.                int var10002 = this.columns * (this.barWidth + 2 * this.barSpacing);
  151.                cx += Math.max((((Component)this).size().width - var10002) / 2, 0);
  152.                int var10001 = this.values[var8];
  153.                int cy = ((Component)this).size().height - var10001 * this.scale - 1 - 2 * this.titleFont.getSize();
  154.                g.setColor(Color.black);
  155.                g.drawString((String)this.labels[var8], cx, ((Component)this).size().height - this.titleFont.getSize() - this.titleFontMetrics.getDescent());
  156.                if (this.colors[var8] == Color.black) {
  157.                   g.setColor(Color.gray);
  158.                }
  159.  
  160.                g.fillRect(cx + 5, cy - 3, this.barWidth, this.values[var8] * this.scale);
  161.                g.setColor((Color)this.colors[var8]);
  162.                switch (this.styles[var8]) {
  163.                   case 0:
  164.                   default:
  165.                      g.fillRect(cx, cy, this.barWidth, this.values[var8] * this.scale);
  166.                      break;
  167.                   case 1:
  168.                      int steps = this.values[var8] * this.scale / 2;
  169.  
  170.                      for(int j = 0; j < steps; ++j) {
  171.                         int ys = cy + 2 * j;
  172.                         g.drawLine(cx, ys, cx + this.barWidth, ys);
  173.                      }
  174.                }
  175.  
  176.                g.drawString("" + this.values[var8], cx, cy - this.titleFontMetrics.getDescent());
  177.                break;
  178.             case 1:
  179.                int cy = (this.barWidth + this.barSpacing) * var8 + this.barSpacing;
  180.                int cx = this.maxLabelWidth + 1;
  181.                cx += Math.max((((Component)this).size().width - (this.maxLabelWidth + 1 + this.titleFontMetrics.stringWidth("" + this.max) + this.max * this.scale)) / 2, 0);
  182.                g.setColor(Color.black);
  183.                g.drawString((String)this.labels[var8], cx - this.maxLabelWidth - 1, cy + this.titleFontMetrics.getAscent());
  184.                if (this.colors[var8] == Color.black) {
  185.                   g.setColor(Color.gray);
  186.                }
  187.  
  188.                g.fillRect(cx + 3, cy + 5, this.values[var8] * this.scale, this.barWidth);
  189.                g.setColor((Color)this.colors[var8]);
  190.                switch (this.styles[var8]) {
  191.                   case 0:
  192.                   default:
  193.                      g.fillRect(cx, cy, this.values[var8] * this.scale, this.barWidth);
  194.                      break;
  195.                   case 1:
  196.                      int steps = this.values[var8] * this.scale / 2;
  197.  
  198.                      for(int j = 0; j < steps; ++j) {
  199.                         int ys = cx + 2 * j;
  200.                         g.drawLine(ys, cy, ys, cy + this.barWidth);
  201.                      }
  202.                }
  203.  
  204.                g.drawString("" + this.values[var8], cx + this.values[var8] * this.scale + 3, cy + this.titleFontMetrics.getAscent());
  205.          }
  206.       }
  207.  
  208.    }
  209. }
  210.